package controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import interceptor.CategoryNavbarInterceptor;
import model.Category;
import model.Reply;
import model.Topic;

import java.util.List;

/**
 * Created by Administrator on 2018/5/18 0018.
 */
public class TopicController extends Controller {
    @Before(CategoryNavbarInterceptor.class)
    public void index() {
        Integer id = getParaToInt(0, -1);
        Topic topic = Topic.dao.findById(id);
        if (topic == null) {
            renderHtml("指定的帖子不存在！");
            return;
        } else {
            String sql = "SELECT\n" +
                    "t_reply.*,\n" +
                    "t_user.nickname\n" +
                    "FROM\n" +
                    "t_reply\n" +
                    "INNER JOIN t_user ON t_reply.user_id = t_user.id\n" +
                    "WHERE topic_id = ?\n";
            List<Reply> replies = Reply.dao.find(sql, id);
            setAttr("topic", topic);
            setAttr("replies", replies);
            renderFreeMarker("view.ftl");
        }


    }

    public void reply() {
        Integer userId = getParaToInt("user_id", -1);
        String content = getPara("content", "");
        Integer topicId = getParaToInt("topic_id", -1);
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setUserId(userId);
        reply.setTopicId(topicId);

        boolean success = false;
        try {
            reply.save();
            success = true;
        } catch (Exception e) {
            LogKit.error("回复帖子失败，原因:" + e.getMessage());
        }
        String message = success ? "成功" : "失败";
        renderHtml("回复" + message);
    }

    public void captcha(){
        renderCaptcha();
    }

    public void topic_list() {
        String user_id = getPara("user_id");
        List<Topic> topics = Topic.dao.find("SELECT * FROM t_topic WHERE user_id = ?",user_id);
        setAttr("topics", topics);
        renderFreeMarker("topic-list.ftl");
    }
    public void topic_modify() {
        Integer id = getParaToInt("id");
       // String id = getPara("id");
        System.out.println(id);
        List<Topic> topics = Topic.dao.find("SELECT * FROM t_topic WHERE id = ?",id);
        setAttr("topics", topics);
        renderFreeMarker("topic_modify.ftl");
    }
    public void save() {
        Integer id = getParaToInt("id");
        System.out.println(id);
        String title = getPara("title");
        String content = getPara("content");
        boolean success = false; //更新成功的标志变量
        try {
            Db.update("UPDATE t_topic SET title = ?,content = ? WHERE id = ?",title,content, id);
            success = true;
        } catch (Exception e) {
            LogKit.error("修改主题失败，原因：" + e.getMessage());
        }
        String message = success ? "修改主题成功" : "修改主题失败";
        renderHtml(message + "<br><a href='/main'>返回主页</a>");
    }
    public void topic_delete() {
        // Integer id = getParaToInt("id", -1);
        Integer id = getParaToInt("id");
        boolean success = false; //删除成功的标志变量
        try {
            Topic.dao.deleteById(id);
            success = true;
        } catch (Exception e) {
            LogKit.error("主题删除失败，原因：" + e.getMessage());
        }
        String message = success ? "主题删除成功" : "主题删除失败";
        renderHtml(message + "<br><a href='/main'>返回主页</a>");
    }
    public void topic_add() {
        List<Category> categories = Category.dao.find("SELECT * FROM t_category");
        setAttr("categories", categories);
        renderFreeMarker("topic_add.ftl");
    }
    public void topicadd() {
        Integer id = getParaToInt("id");
        String title = getPara("title");
        String content = getPara("content");
        Integer cid = getParaToInt("c");
       Topic topic = new Topic();

        topic.setUserId(id);
        topic.setCategoryId(cid);
        topic.setTitle(title);
        topic.setContent(content);

        boolean success = false; //添加成功的标志变量
        try {
            topic.save();
            success = true;
        } catch (Exception e) {
            LogKit.error("主题添加失败，原因：" + e.getMessage());
        }
        String message = success ? "主题添加成功" : "主题添加失败";
        renderHtml(message + "<br><a href='/category/list'>返回主页</a>");

    }


    public void reply_modify() {
        Integer id = getParaToInt("id");
       // String id = getPara("id");
        System.out.println(id);

        String content = getPara("content");
        System.out.println(content);
        boolean success = false; //更新成功的标志变量
        try {
            Db.update("UPDATE t_reply SET content = ? WHERE id = ?", content, id);
            success = true;
        } catch (Exception e) {
            LogKit.error("回复更新失败，原因：" + e.getMessage());
        }
        String message = success ? "回复更新成功" : "回复更新失败";
        renderHtml(message + "<br><a href='/main'>返回主页</a>");
    }
    public void reply_delete() {
        String id = getPara("id");
        boolean success = false; //删除成功的标志变量
        try {
            Reply.dao.deleteById(id);
            success = true;
        } catch (Exception e) {
            LogKit.error("回复删除失败，原因：" + e.getMessage());
        }
        String message = success ? "回复删除成功" : "回复删除失败";
        renderHtml(message + "<br><a href='/main'>返回主页</a>");
    }
    public void reply_list() {
        Integer id = getParaToInt("user_id");
        List<Reply> replys =Reply.dao.find("SELECT * FROM t_reply Where user_id=?",id);
        setAttr("replys", replys);
        renderFreeMarker("reply-list.ftl");
    }
}
